import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * A Servlet to help view jpg images
 *
 * @author James Chy
 */

public class Search extends PhotoServlet {

    static final long serialVersionUID = 0;
    
    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
    }
        
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        out = response.getWriter();


        out.println("<html>");
        out.println("<body bgcolor=\"white\">");
        out.println("<head>");

		out.println("<title>Search</title>");  
        out.println("</head>");
        out.println("<body>");

        try
        {
		    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
            java.sql.Connection conn;
            conn = DriverManager.getConnection(connection);

            String query;
            Statement stmt;
            ResultSet rs;

            out.println("<FORM ACTION=\"SearchResults\" METHOD=\"GET\">");

            out.println("<TABLE><TR><TD>");

            // Begin Dates

            query = "SELECT uid, date " +
                    "FROM dates " +
                    "ORDER BY date DESC";
            
            if(showQuery)
                out.println("Query:<BR>" + query + "<BR>");

            stmt = conn.createStatement();
            rs = stmt.executeQuery(query);

            if(maintdates)
               out.println("<B><A HREF=\"MaintainDates\" TARGET=\"_blank\">Dates</A></B><BR>");
            else
			   out.println("<B>Dates</B><BR>");
            
            out.println("<SELECT NAME=\"dates\" SIZE=\"20\" MULTIPLE>");
            out.println("<OPTION SELECTED VALUE=\"0\">&lt;None&gt;");
            while (rs.next()) 
            {
                String uid = rs.getString("uid");
                String value = rs.getString("date");    
                
                out.print("<OPTION ");
                out.println("VALUE=\"" + uid + "\">" + Search.MonthDayYear(value));
            }
            out.println("</SELECT>");

            // End Dates           

            out.println("</TD><TD>");

            // Begin People

            query = "SELECT people.* " +
                    "FROM people " +
                    "ORDER BY lastName, firstName";
            
            if(showQuery)
                out.println("Query:<BR>" + query + "<BR>");

            stmt = conn.createStatement();
            rs = stmt.executeQuery(query);

            if(maintpeople)
               out.println("<B><A HREF=\"MaintainPeople\" TARGET=\"_blank\">People</A></B><BR>");
            else
               out.println("<B>People</B><BR>");
               
            out.println("<SELECT NAME=\"people\" SIZE=\"20\" MULTIPLE>");
            out.println("<OPTION SELECTED VALUE=\"0\">&lt;None&gt;");
            while (rs.next()) 
            {
                String uid = rs.getString("uid");

                String value=Search.FirstNameLastName(rs);

                out.print("<OPTION ");
                out.println("VALUE=\"" + uid + "\">" + value);
            }
            out.println("</SELECT>");

            // End People

            out.println("</TD><TD>");

            // Begin Things

            query = "SELECT uid, thing " +
                    "FROM things " +
                    "ORDER BY thing";
            
            if(showQuery)
                out.println("Query:<BR>" + query + "<BR>");

            stmt = conn.createStatement();
            rs = stmt.executeQuery(query);

            if(maintthings)
               out.println("<B><A HREF=\"MaintainThings\" TARGET=\"_blank\">Things</A></B><BR>");
            else
               out.println("<B>Things</B><BR>");
            
            out.println("<SELECT NAME=\"things\" SIZE=\"20\" MULTIPLE>");
            out.println("<OPTION SELECTED VALUE=\"0\">&lt;None&gt;");
            while (rs.next()) 
            {
                String uid = rs.getString("uid");
                String value = rs.getString("thing");
                out.print("<OPTION ");
                out.println("VALUE=\"" + uid + "\">" + value);
            }
            out.println("</SELECT>");

            // End Things

            out.println("</TD><TD>");

            // Begin Places

            query = "SELECT uid, place " +
                    "FROM places " +
                    "ORDER BY place";
            
            if(showQuery)
                out.println("Query:<BR>" + query + "<BR>");

            stmt = conn.createStatement();
            rs = stmt.executeQuery(query);

            if(maintplaces)
               out.println("<B><A HREF=\"MaintainPlaces\" TARGET=\"_blank\">Places</A></B><BR>");
            else
			out.println("<B>Places</B><BR>");
            out.println("<SELECT NAME=\"places\" SIZE=\"20\" MULTIPLE>");
            out.println("<OPTION SELECTED VALUE=\"0\">&lt;None&gt;");
            while (rs.next()) 
            {
                String uid = rs.getString("uid");
                String value = rs.getString("place");
                out.print("<OPTION ");
                out.println("VALUE=\"" + uid + "\">" + value);
            }
            out.println("</SELECT>");

            // End Places

            out.println("</TR></TABLE>");
            out.println("<BR><BR>");
            out.println("<INPUT TYPE=HIDDEN NAME=sessionid VALUE=0>");
            out.println("<INPUT TYPE=SUBMIT NAME=\"Search\" VALUE=\"Search\">");
            if(canfindnew)
               out.println("Find New <INPUT TYPE=CHECKBOX NAME=\"FindNew\">");
            out.println("No Items <INPUT TYPE=CHECKBOX NAME=\"NoItems\">");
            out.println("</FORM>");
        }
        catch(ClassNotFoundException cnfe)
        {
            out.println("Class Not Found Exception " + cnfe.getMessage());
        }
        
        catch(InstantiationException ie)
        {
            out.println("Instantiation Exception " + ie.getMessage());
        }	
        catch(IllegalAccessException iae)
        {
            out.println("Illegal Access Exception " + iae.getMessage());
        }
        catch(SQLException sqle)
        {
            out.println("SQL Exception " + sqle.getMessage());
        }
        
        out.println("</body>");
        out.println("</html>");

    }
               
}



